using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The SpeakLanguageCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about SpeakLanguageCodeTable objects.
    /// </summary>
    public class SpeakLanguageCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of SpeakLanguageCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="speakLanguageCode">The unique SpeakLanguageCode of the SpeakLanguageCodeTable in the database.</param>
        /// <returns>An SpeakLanguageCodeTable when the SpeakLanguageCode was found in the database, or null otherwise.</returns>
        public static SpeakLanguageCodeTable GetItem(short speakLanguageCode)
        {
            SpeakLanguageCodeTable mySpeakLanguageCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSpeakLanguageCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@speakLanguageCode", speakLanguageCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            mySpeakLanguageCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return mySpeakLanguageCodeTable;
            }
        }

        /// <summary>
        /// Returns a list with SpeakLanguageCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the SpeakLanguageCodeTable objects.</returns>
        public static SpeakLanguageCodeTableList GetList()
        {
            SpeakLanguageCodeTableList tempList = new SpeakLanguageCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSpeakLanguageCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a SpeakLanguageCodeTable in the database.
        /// </summary>
        /// <param name="mySpeakLanguageCodeTable">The SpeakLanguageCodeTable instance to save.</param>
        /// <returns>The new SpeakLanguageCode if the SpeakLanguageCodeTable is new in the database or the existing SpeakLanguageCode when an item was updated.</returns>
        public static int Save(SpeakLanguageCodeTable mySpeakLanguageCodeTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSpeakLanguageCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (mySpeakLanguageCodeTable.SpeakLanguageCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@speakLanguageCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@speakLanguageCode", mySpeakLanguageCodeTable.SpeakLanguageCode);
                    }
                    if (string.IsNullOrEmpty(mySpeakLanguageCodeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", mySpeakLanguageCodeTable.Description);
                    }
                    if (mySpeakLanguageCodeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", mySpeakLanguageCodeTable.Status);
                    }
                    if (mySpeakLanguageCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", mySpeakLanguageCodeTable.CreatedDateTime);
                    }
                    if (mySpeakLanguageCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", mySpeakLanguageCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a SpeakLanguageCodeTable from the database.
        /// </summary>
        /// <param name="speakLanguageCode">The SpeakLanguageCode of the SpeakLanguageCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short speakLanguageCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSpeakLanguageCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@speakLanguageCode", speakLanguageCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the SpeakLanguageCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static SpeakLanguageCodeTable FillDataRecord(IDataRecord myDataRecord)
        {
            SpeakLanguageCodeTable mySpeakLanguageCodeTable = new SpeakLanguageCodeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SpeakLanguageCode")))
            {
                mySpeakLanguageCodeTable.SpeakLanguageCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("SpeakLanguageCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                mySpeakLanguageCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                mySpeakLanguageCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                mySpeakLanguageCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                mySpeakLanguageCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return mySpeakLanguageCodeTable;
        }
    }

}
